package com.tfswue.ershoujiaoyi.controller;

import com.tfswue.ershoujiaoyi.entity.Order;
import com.tfswue.ershoujiaoyi.service.OrderService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "订单管理", description = "订单的创建、查询、修改和删除")
@RestController
@RequestMapping("/api/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Operation(summary = "创建订单", description = "用户提交购买请求，生成新订单")
    @PostMapping("/create")
    public Order createOrder(@RequestBody Order order) {
        return orderService.createOrder(order);
    }

    @Operation(summary = "查询订单列表（按买家）", description = "根据买家ID查询自己的所有订单")
    @GetMapping("/list")
    public List<Order> listOrdersByBuyer(
            @Parameter(description = "买家用户ID", required = true) @RequestParam Long buyerId) {
        return orderService.listOrdersByBuyer(buyerId);
    }

    @Operation(summary = "查询所有订单", description = "查询全部订单（管理用）")
    @GetMapping("/all")
    public List<Order> listAll() {
        return orderService.listAll();
    }

    @Operation(summary = "根据ID查询订单", description = "根据订单ID查询订单详细信息")
    @GetMapping("/{id}")
    public Order getById(@Parameter(description = "订单ID", required = true) @PathVariable Long id) {
        return orderService.getById(id);
    }

    @Operation(summary = "修改订单信息", description = "根据订单ID修改订单信息（如状态等）")
    @PutMapping("/update")
    public Order update(@RequestBody Order order) {
        return orderService.update(order);
    }

    @Operation(summary = "删除订单", description = "根据订单ID删除订单")
    @DeleteMapping("/delete/{id}")
    public String delete(@Parameter(description = "订单ID", required = true) @PathVariable Long id) {
        boolean success = orderService.delete(id);
        return success ? "删除成功" : "删除失败";
    }
}
